$(".datebox").datebox({
	formatter: formatDate,
	'setValue':'today'
});

$('#btnSend_'+type).bind('click', function(){
	submit_form(type+'_form', function(resp){
		if(resp == 1){
			kembali(type);
			$.messager.alert('Sukses','Data Tersimpan','Sukses');
		}else{
			//$.messager.alert('Gagal','Data Gagal Tersimpan','Gagal');
			$.messager.alert('Gagal',resp,'Gagal');
		}
		
	})
});

function kembali(typenya){
	_post = [{name:'type', value: typenya}];		
	$('#master_'+typenya).html('');
	$('#master_'+typenya).addClass("loading");
	$.post(host+'master/gettab/', _post, function(resp) {
		$('#master_'+typenya).removeClass('loading')
		$('#master_'+typenya).html(resp);							
	});	
}

$('#get_nama_barang_'+type).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':'type_supply_form', 'type_window':'barang'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});

$('#id_group_dealer').change(function() {
	if ($('#id_group_dealer').val() == "") {
		$('#id_dealer').html("<option value=''>-- Pilih --</option>");
		$('#id_kas').html("<option value=''>-- Pilih --</option>");
	} else {
		$('#id_dealer').empty();
		$('#id_kas').html("<option value=''>-- Pilih --</option>");
		fillCombo(host+'master/combobox/tbl_master_dealer/', 'id_dealer', "echo", $('#id_group_dealer').val());
	}
});

$('#id_group_barang').change(function() {
	$('#id_kategori_barang').empty();
	fillCombo(host+'master/combobox/idx_kategori_barang/', 'id_kategori_barang', "echo", $('#id_group_barang').val());
});

$('#id_group_service').change(function() {
	$('#id_kategori_service').empty();
	fillCombo(host+'master/combobox/idx_kategori_service/', 'id_kategori_service', "echo", $('#id_group_service').val());
});

function currencyFormat(obj) {
	$(obj).parseNumber({format:"#,###.00", locale:"us"});
	$(obj).formatNumber({format:"#,###.00", locale:"us"});
}

if(type == "karyawan") {
	$('#tanggal_masuk_kerja').datebox({
		formatter:formatDate,
		value:tglmulaikerja
	});
}

if(type == "dealer" || type == "supplier") {
	if(editstatus == 'edit'){
		$.post(host+'master/getdatatambahan/wilayah_dealer/', {'param1' : $('#id_wilayah').val()}, function(resp) {
			data = jQuery.parseJSON(resp);
			$('#prov_dealer').val(data.nama_provinsi);
			$('#kab_dealer').val(data.nama_kabupaten);
			$('#kec_dealer').val(data.nama_kecamatan);
			//$('#kel_dealer').val(data.nama_kelurahan);
			$('#kd_pos').val(data.kode_pos);
		});	
	}
	
	$('#id_wilayah').on('change', function(){
		$.post(host+'master/getdatatambahan/wilayah_dealer/', {'param1':$(this).val()}, function(resp) {
			if(resp){
				data = jQuery.parseJSON(resp);
				$('#prov_dealer').val(data.nama_provinsi);
				$('#kab_dealer').val(data.nama_kabupaten);
				$('#kec_dealer').val(data.nama_kecamatan);
				//$('#kel_dealer').val(data.nama_kelurahan);
				$('#kd_pos').val(data.kode_pos);
			}else{
				$('#prov_dealer').html('');
				$('#kab_dealer').html('');
				$('#kec_dealer').html('');
				$('#kel_dealer').html('');
				$('#kd_pos').html('');
			}
		});	
	});
}

// motor, spare part, generator, peralatan service, barang promosi
if (type == "motor" || 
	type == "sparepart" || 
	type == "generator" || 
	type == "peralatan_service" || 
	type == "barang_promosi") {
	var idx = 1;
	
	function add_hrg_jual() {
		idx++;
		var row_na = " <tr id='hrg_jual_"+type+"_"+idx+"'><td> <select id='tingkat_hrg_jual_1' name='tingkat_hrg_jual[]' " +
					 "style='width: 100%; border: 0; padding: 1px; background: none!important;'>" + tingkathrgjual + " </select></td> " + 
					 
					 " <td> <input type='text' id='nom_hrg_jual_"+idx+"' name='nom_hrg_jual[]' " + 
					 " onblur=\"calculateVal('nom_hrg_jual','"+idx+"');\" " +
					 " style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;'> </td> " +
					 
					 " <td> <select id='kondisi_ppn_hrg_jual_"+idx+"' name='kondisi_ppn_hrg_jual[]' " +
					 " style='width: 100%; border: 0; padding: 1px; background: none!important;'> "+kondisippnjual+" </select> </td> " +
					 
					 " <td> <input type='text' id='dpp_hrg_jual_"+idx+"' name='dpp_hrg_jual[]' disabled='disabled' " +
					 " style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;'> </td> " +
					
					 " <td> <input type='text' id='ppn_hrg_jual_"+idx+"' name='ppn_hrg_jual[]' " +
					 " onblur=\"calculateVal('ppn_hrg_jual','"+idx+"');\" " +
					 " style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;'> </td> " +
					
					 " <td> <input type='text' id='nom_ppn_hrg_jual_"+idx+"' name='nom_ppn_hrg_jual[]' disabled='disabled' " +
					 " style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;'> </td> " +
					
					 " <td> <input type='text' id='hrg_jual_plus_ppn_"+idx+"' name='hrg_jual_plus_ppn[]' disabled='disabled' " + 
					 " style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;'> </td> " +
								
					 " <td align='center'><a href='#'  onclick='add_hrg_jual();' title='Tambah'>" + 
					 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
					 " onclick='removeRow("+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
					 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
					 "</tr> ";
		$(".tab_body").append(row_na);
		//alert('hahahah');
	}
	
	function removeRow(index){
   	 	$('#hrg_jual_'+type+'_'+index).remove();
	}

	function removeEditRow(idxs, ids) {
		$.post(host+'master/removedata/hargajual',{'id':ids},function(resp) {
			if (resp == '1') {
				$("#hrgjual_"+idxs).hide();
			}
		});
	}
	
	// nominal harga beli handler
	function changebeli(nom_beli,ppn_beli) {
		$("#dpp_harga_beli").val((nom_beli / 1.1));
		$("#dpp_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
		$("#dpp_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
		
		$("#nominal_ppn_harga_beli").val( ((nom_beli / 1.1) * (ppn_beli / 100)) );
		$("#nominal_ppn_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
		$("#nominal_ppn_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
		
		$("#harga_beli_plus_ppn").val( (((nom_beli / 1.1) * (ppn_beli / 100)) +  (nom_beli / 1.1)) );
		$("#harga_beli_plus_ppn").parseNumber({format:"#,###.00", locale:"us"});
		$("#harga_beli_plus_ppn").formatNumber({format:"#,###.00", locale:"us"});
	}

	$("#nominal_harga_beli").blur(function() {
		$(this).parseNumber({format:"#,###.00", locale:"us"});
		$(this).formatNumber({format:"#,###.00", locale:"us"});
		
		var nom_beli = $(this).val().replace(/[$,]+/g,"");
		var ppn_beli = $("#ppn_harga_beli").val().replace(/[$,]+/g,"");
		
		changebeli(nom_beli,ppn_beli);
	});

	$("#ppn_harga_beli").blur(function() {
		$(this).parseNumber({format:"#,###.00", locale:"us"});
		$(this).formatNumber({format:"#,###.00", locale:"us"});
		
		var nom_beli = $("#nominal_harga_beli").val().replace(/[$,]+/g,"");
		var ppn_beli = $(this).val().replace(/[$,]+/g,"");
		
		changebeli(nom_beli,ppn_beli);
	});
	
	function changejual(nom_jual,ppn_jual, ids) {
		$("#dpp_hrg_jual_"+ids).val((nom_jual / 1.1));
		$("#dpp_hrg_jual_"+ids).parseNumber({format:"#,###.00", locale:"us"});
		$("#dpp_hrg_jual_"+ids).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#nom_ppn_hrg_jual_"+ids).val( ((nom_jual / 1.1) * (ppn_jual / 100)) );
		$("#nom_ppn_hrg_jual_"+ids).parseNumber({format:"#,###.00", locale:"us"});
		$("#nom_ppn_hrg_jual_"+ids).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#hrg_jual_plus_ppn_"+ids).val( (((nom_jual / 1.1) * (ppn_jual / 100)) +  (nom_jual / 1.1)) );
		$("#hrg_jual_plus_ppn_"+ids).parseNumber({format:"#,###.00", locale:"us"});
		$("#hrg_jual_plus_ppn_"+ids).formatNumber({format:"#,###.00", locale:"us"});
	}
	
	function calculateVal(tagid, ids) {
		var tags = '#'+tagid+'_'+ids;
		$(tags).parseNumber({format:"#,###.00", locale:"us"});
		$(tags).formatNumber({format:"#,###.00", locale:"us"});
		
		if (tagid == 'nom_hrg_jual') {
			var nom_jual = $(tags).val().replace(/[$,]+/g,"");
			var ppn_jual = $("#ppn_hrg_jual_"+ids).val().replace(/[$,]+/g,"");
		} else {
			var nom_jual = $("#nom_hrg_jual_"+ids).val().replace(/[$,]+/g,"");
			var ppn_jual = $(tags).val().replace(/[$,]+/g,"");
		}
		changejual(nom_jual,ppn_jual, ids);
	}

	function numberFormat(num) {
		$("#nom_hrg_jual_" + num).parseNumber({format:"#,###.00", locale:"us"});
		$("#nom_hrg_jual_" + num).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#ppn_hrg_jual_" + num).parseNumber({format:"#,###.00", locale:"us"});
		$("#ppn_hrg_jual_" + num).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#dpp_hrg_jual_" + num).parseNumber({format:"#,###.00", locale:"us"});
		$("#dpp_hrg_jual_" + num).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#nom_ppn_hrg_jual_" + num).parseNumber({format:"#,###.00", locale:"us"});
		$("#nom_ppn_hrg_jual_" + num).formatNumber({format:"#,###.00", locale:"us"});
		
		$("#hrg_jual_plus_ppn_" + num).parseNumber({format:"#,###.00", locale:"us"});
		$("#hrg_jual_plus_ppn_" + num).formatNumber({format:"#,###.00", locale:"us"});
	}
	
	if(editstatus == 'edit') {
		$(document).ready(function() {
			$("#nominal_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
			$("#nominal_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
			
			$("#ppn_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
			$("#ppn_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
		
			$("#dpp_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
			$("#dpp_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
			
			$("#nominal_ppn_harga_beli").parseNumber({format:"#,###.00", locale:"us"});
			$("#nominal_ppn_harga_beli").formatNumber({format:"#,###.00", locale:"us"});
			
			$("#harga_beli_plus_ppn").parseNumber({format:"#,###.00", locale:"us"});
			$("#harga_beli_plus_ppn").formatNumber({format:"#,###.00", locale:"us"});
		});
		fillCombo(host+'master/combobox/idx_kategori_barang/'+kategoribarang,'id_kategori_barang', "echo", $('#id_group_barang').val());
	}
}

if (type == "service") {
	if(editstatus == 'edit'){
		fillCombo(host+'master/combobox/idx_kategori_service/'+kategoriservice,'id_kategori_service', "echo", $('#id_group_service').val());
	}
}

if ( type == "proses_promosi_unit" || 
	type == "proses_promosi_service" || 
	type == "proses_promosi_sparepart" ||
	type == "proses_pembiayaan_unit" ) {
	var idx = 1;
	

	// on load in editing mode
	if (editstatus == 'edit') {
		$(".getCurrency").formatNumber({format:"#,###.00", locale:"us"});

		if (type == "proses_promosi_unit") {
			if(promosi_all_dealer == 'N') {
				$("#promosi_dealer").show();	
			}

			if(jenis_penjualan != "") {
				//alert('hahaha');
				$("#data_support").show();
				if (jenis_penjualan == "KD" || jenis_penjualan == "T") {
					$("#kredit_dealer").show();
				} else if (jenis_penjualan == "KL") {
					$("#kredit_leasing").show();
					$("#pembiayaan").css({'background':'#ffffff'}).prop('disabled', false);
				} else {
					// do nothing
				}
			}
		}

		if (type == "proses_promosi_service") {
			fillCombo(host+'master/combobox/tbl_master_dealer/'+id_dealer,'id_dealer', "echo", $("#id_group_dealer").val());
			fillCombo(host+'master/combobox/idx_kategori_barang/'+kategori_brg,'id_kategori_barang', "echo", $("#id_group_barang").val());
			fillCombo(host+'master/combobox/idx_kategori_service/'+kategori_service,'id_kategori_service', "echo", $("#id_group_service").val());
		}

		if (type == "proses_pembiayaan_unit") {
			fillCombo(host+'master/combobox/tbl_master_supplier/'+id_supplier,'id_supplier', "echo", $("#id_group_supplier").val());
			
			if (jenis_support == "S") {
				$(".support_subsidi").show();
			} else if (jenis_support == "I") {
				$(".support_incentive").show();
			} else if (jenis_support == "P") {
				$(".support_promo").show();
			} else {
				// do nothing
			}
		}

		var tgl_mulai_promo = $("#tgl_mulai").val();
		var tgl_berakhir_promo = $("#tgl_berakhir").val();
		var dmulai = new Date(tgl_mulai_promo.split('-')[0],tgl_mulai_promo.split('-')[1]-1,tgl_mulai_promo.split('-')[2]);
		var dakhir = new Date(tgl_berakhir_promo.split('-')[0],tgl_berakhir_promo.split('-')[1]-1,tgl_berakhir_promo.split('-')[2]);
		var timeDiff = Math.abs(dakhir.getTime() - dmulai.getTime());
		var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
		
		var bulan = Math.floor(diffDays / 30);
		var hari  = Math.abs(diffDays % 30);
		
		$("#masa_berlaku_bln").val(bulan);
		$("#masa_berlaku_hari").val(hari);
	}

	
	$("#id_group_supplier").change(function() {
		fillCombo(host+'master/combobox/tbl_master_supplier','id_supplier', "echo", $("#id_group_supplier").val());
	});

	
	function getDealer(group, dealer, tagid) {
		fillCombo(host+'master/combobox/tbl_master_dealer/'+dealer,'promosi_dealer_'+tagid, "echo", group);
	}

	function getpromodealer(obj) {
		if($(obj).val() == 'N') {
			$("#promosi_dealer").show();
		} else {
			$("#promosi_dealer").hide();
		}
	}
	
	function addRows(typex) {
		idx++;
		switch(typex) {
			case 'dealer':
				var rows = "<tr id='promodealer_"+idx+"' > <td>" +
							"<select id='group_promosi_dealer_"+idx+"' onchange='changeGroupDealer(\""+idx+"\");' name='group_promosi_dealer[]' style='width: 100%; border: 0; padding: 1px; background: none!important;'" +
							"<option>-- Pilih --</option>" +promodealer +
							" </select> </td>" +
							" <td> <select id='promosi_dealer_"+idx+"' name='promosi_dealer[]' style='width: 100%; border: 0; padding: 1px; background: none!important;'>" +
							" <option>-- Pilih --</option> </select> </td>" +
							
							" <td align='center'><a href='#'  onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body";
			break;
			case 'hadiah':
				var rows = "<tr id='datahadiah_"+idx+"' >" +
							"<td> <input type='text' id='brg_promosi_"+idx+"' name='brg_promosi[]' style='width: 100%; border: 0; padding: 1px; background: none!important;' /> </td> " +
							"<td> <input type='text' id='brg_hadiah_a_"+idx+"' name='brg_hadiah_a[]' style='width: 100%; border: 0; padding: 1px; background: none!important;' /> </td> " +
							"<td> <input type='text' id='qty_hadiah_a_"+idx+"' name='qty_hadiah_a[]' style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;' /> </td> " +
							"<td> <input type='text' id='brg_hadiah_b_"+idx+"' name='brg_hadiah_b[]' style='width: 100%; border: 0; padding: 1px; background: none!important;' /> </td> " +
							"<td> <input type='text' id='qty_hadiah_b_"+idx+"' name='qty_hadiah_b[]' style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;' /> </td> " +
							"<td> <input type='text' id='brg_hadiah_c_"+idx+"' name='brg_hadiah_c[]' style='width: 100%; border: 0; padding: 1px; background: none!important;' /> </td> " +
							"<td> <input type='text' id='qty_hadiah_c_"+idx+"' name='qty_hadiah_c[]' style='width: 100%; border: 0; padding: 1px; background: none!important; text-align: right;' /> </td>" +
							
							"<td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							"<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							" onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							"<input type='hidden' name='id_detil2[]' value='' ><input type='hidden' name='sts_detil[]2' value='add' > " + 
							"</td></tr> ";
				var tags = ".tab_body_hadiah";
			break;
			case 'kd':
				var rows = "<tr id=\"kreditdealer_"+idx+"\" >" +
							"<td> <input type='text' id='nama_barang_KD_"+idx+"' name='nama_barang_KD[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" />" +
							"</td> <td> <input type='text' id='nilai_discount_"+idx+"' name='nilai_discount[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> </td>"+
							
							"<td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							"<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							" onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							"<input type='hidden' name='id_detil3[]' value='' ><input type='hidden' name='sts_detil[]3' value='add' > " + 
							"</td></tr> ";
				var tags = ".tab_body_kd";
			break;
			case 'kl':
				var rows = "<tr id=\"kreditleasing_"+idx+"\" >" +
							"<td> <input type='text' id='nama_barang_KL_"+idx+"' name='nama_barang_KL[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' id='kondisi_uang_muka_"+idx+"' name='kondisi_uang_muka[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> </td>" +
							"<td> <input type='text' id='tenor_a_"+idx+"' name='tenor_a[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> </td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_a_"+idx+"' name='nilai_subsidi_a[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> </td>" +
							"<td> <input type='text' id='tenor_b_"+idx+"' name='tenor_b[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_b_"+idx+"' name='nilai_subsidi_b[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' id='tenor_c_"+idx+"' name='tenor_c[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_c_"+idx+"' name='nilai_subsidi_c[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' id='tenor_d_"+idx+"' name='tenor_d[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_d_"+idx+"' name='nilai_subsidi_d[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' id='tenor_e_"+idx+"' name='tenor_e[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_e_"+idx+"' name='nilai_subsidi_e[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> </td>" +
							"<td> <input type='text' id='tenor_f_"+idx+"' name='tenor_f[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							"<td> <input type='text' class=\"getCurrency\" onchange=\"currencyFormat(this);\" id='nilai_subsidi_f_"+idx+"' name='nilai_subsidi_f[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /></td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil4[]' value='' ><input type='hidden' name='sts_detil4[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_kl";
			break;
			case 'hadiah_lsg':
				var rows = "<tr id=\"hadiah_lsg_"+idx+"\" >" +
							"<td> <select id='id_group_barang' name='id_group_barang' style=\"width:170px; background: transparent; border: none; padding: 1px;\">"+
							optgroupbarang+
							"</select> </td> <td> <input type='text' id='nama_barang_1' name='nama_barang[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" />"+
							" </td> <td> <input type='text' id='qty_1' name='qty[]' "+
							" style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" />"+
							"</td> <td><select id='satuan' name='satuan' style=\"width:170px; border: none; padding: 1px;\">"+
							optsatuan+
							"</select> </td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_hadiah_lsg";
			break;
			case 'paket':
				var rows = "<tr id=\"paket_"+idx+"\" >"+
							"<td> <input type='text' id='kode_barang_paket_"+idx+"' name='kode_barang_paket[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" />"+
							"</td> <td>"+
							"<input type='text' id='nama_barang_paket_"+idx+"' name='nama_barang_paket[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" />"+
							"</td> <td>"+
							"<input type='text' id='harga_jual_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='harga_jual[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" />"+
							"</td> <td>"+
							"<input type='text' id='quantity_"+idx+"' name='quantity[]' style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" />"+
							"</td> <td>"+
							"<select id='satuan_barang_"+idx+"' name='satuan_barang[]' style=\"width: 100%; border: none; padding: 1px; background: transparent; \">"+
							optsatuan+
							"</select> </td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_paket";
			break;
			case 'support_subsidi':
				var rows = "<tr id=\"support_subsidi_"+idx+"\" >"+
							"<td> <input type='text' id='subsidi_nm_brg_"+idx+"' name='subsidi_nm_brg[]' "+
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" />"+
							"</td> <td>"+
							"<input type='text' id='subsidi_tgl_mulai_"+idx+"' name='subsidi_tgl_mulai[]' class=\"datebox\""+
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" />"+
							"</td> <td>"+
							"<input type='text' id='subsidi_tgl_akhir_"+idx+"' name='nama_tgl_akhir[]' class=\"datebox\" "+
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" />"+
							"</td> <td>"+
							"<input type='text' id='subsidi_nilai_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='subsidi_nilai[]' "+
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" />"+
							"</td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_support_subsidi";
				var datetag = "#subsidi_tgl_mulai_"+idx+", #subsidi_tgl_akhir_"+idx;
			break;
			case 'support_sub_add':
				var rows = "<tr id=\"support_sub_add_"+idx+"\" > " +
							"<td> <input type='text' id='sub_add_nm_brg_"+idx+"' name='sub_add_nm_brg[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_tgl_mulai_"+idx+"'bname='subb_add_tgl_mulai[]' class=\"datebox\" " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_tgl_akhir_"+idx+"' name='sub_add_tgl_akhir[]' class=\"datebox\" " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td>" +
							"<input type='text' id='sub_add_min_uang_muka_1' name='sub_add_min_uang_muka[]' " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" /> " +
							"</td><td> " +
							"<input type='text' id='sub_add_tenor_a_"+idx+"' name='subb_add_tenor_a[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_nilai_a_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='sub_add_nilai[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td>  " +
							"<input type='text' id='sub_add_tenor_b_"+idx+"' name='sub_add_tenor_b[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_nilai_b_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='sub_add_nilai_b[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_tenor_c_"+idx+"' name='sub_add_tenor_c[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_nilai_c_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='sub_add_nilai_c[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_tenor_d_"+idx+"' name='sub_add_tenor_d[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_nilai_d_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='sub_add_nilai_d[]'   " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_tenor_e_"+idx+"' name='sub_add_tenor_e[]'   " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td> <td> " +
							"<input type='text' id='sub_add_nilai_e_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='sub_add_nilai_e[]'   " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_support_sub_add";
				var datetag = "#sub_add_tgl_mulai_"+idx+", #sub_add_tgl_akhir_"+idx;
			break;
			case 'support_incentive':
				var rows = "<tr id=\"support_incentive_"+idx+"\" > <td> " +
							"<input type='text' id='incentive_nm_brg_"+idx+"' name='incentive_nm_brg[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" /> " +
							"</td> <td> " +
							"<input type='text' id='incentive_tgl_mulai_"+idx+"' name='incentive_tgl_mulai[]' class=\"datebox\" " +
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td> " +
							"<input type='text' id='incentive_tgl_akhir_"+idx+"' name='incentive_tgl_akhir[]' class=\"datebox\" " +
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td> " +
							"<input type='text' id='incentive_nilai_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='incentive_nilai[]' " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_support_incentive";
				var datetag = "#incentive_tgl_mulai_"+idx+", #incentive_tgl_akhir_"+idx;
			break;
			case 'support_promo':
				var rows = "<tr id=\"support_promo_1\" > " +
							"<td> <input type='text' id='promo_nm_brg_"+idx+"' name='promo_nm_brg[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent;\" /> " +
							"</td> <td> " +
							"<input type='text' id='promo_tgl_mulai_"+idx+"' name='promo_tgl_mulai[]' class=\"datebox\" " +
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td> " +
							"<input type='text' id='promo_tgl_akhir_"+idx+"' name='promo_tgl_akhir[]' class=\"datebox\" " +
							"style=\"width: 150px; border: 0; padding: 1px; background: none!important;\" /> " +
							"</td> <td> " +
							"<input type='text' id='promo_nilai_"+idx+"' class=\"getCurrency\" onchange=\"currencyFormat(this);\" name='promo_nilai[]'  " +
							"style=\"width: 100%; border: 0; padding: 1px; background: none!important; background: transparent; text-align: right;\" /> " +
							"</td>" +
							
							" <td align='center'><a href='#' onclick='addRows(\""+typex+"\");' title='Tambah'>" + 
							 "<img src='"+host+"assets/css/themes_easyui/icons/add.png' /></a>&nbsp;<a href='#'  class='hapus_row' " + 
							 " onclick='removeRows(\""+typex+"\","+idx+")' title='Hapus'><img src='"+host+"assets/css/themes_easyui/icons/delete.png' /></a>" + 
							 "<input type='hidden' name='id_detil[]' value='' ><input type='hidden' name='sts_detil[]' value='add' > " + 
							 "</td></tr> ";
				var tags = ".tab_body_support_promo";
				var datetag = "#promo_tgl_mulai_"+idx+", #promo_tgl_akhir_"+idx;
			break;
		}

		$(tags).append(rows);
		if (typex == 'support_promo' || 
			typex == 'support_incentive' ||
			typex == 'support_subsidi' ||
			typex == 'support_sub_add') {
				$(datetag).datebox({
					formatter: formatDate,
					'setValue':'today'
				});	
		}
	}

	function removeRows(type, index, ids) {
   	 	switch(type) {
   	 		case 'dealer':
   	 			var tags  = '#promodealer';
   	 			var param = 'promodealer';
   	 		break;
   	 		case 'hadiah':
   	 			var tags  = '#datahadiah';
   	 			var param = 'promohadiah';
   	 		break;
   	 		case 'kd':
   	 			var tags  = '#kreditdealer';
   	 			var param = 'kredit_dealer';
   	 		break;
   	 		case 'kl':
   	 			var tags  = '#kreditleasing';
   	 			var param = 'kredit_leasing';
   	 		break;
   	 		case 'hadiah_lsg':
   	 			var tags  = '#hadiah_lsg';
   	 			var param = 'hadiah_lsg';
   	 		break;
   	 		case 'paket':
   	 			var tags  = '#paket';
   	 			var param = 'paket';
   	 		break;
   	 		case 'support_subsidi':
   	 			var tags  = '#support_subsidi';
   	 			var param = 'support_product';
   	 		break;
   	 		case 'support_sub_add':
   	 			var tags  = '#support_sub_add';
   	 			var param = 'support_sub_add';
   	 		break;
   	 		case 'support_incentive':
   	 			var tags  = '#support_incentive';
   	 			var param = 'support_product';
   	 		break;
   	 		case 'support_promo':
   	 			var tags  = '#support_promo';
   	 			var param = 'support_product';
   	 		break;
   	 	}

   	 	if (ids == undefined) {
   	 		$(tags+'_'+index).remove();
   	 	} else {
   	 		$.post(host+'master/removedata/'+param,{'id':ids},function(resp) {
				if (resp == '1') {
					$(tags+'_'+index).hide();
				}
			});
   	 	}
	}

	function changeGroupDealer(id) {
		if($("#group_promosi_dealer_"+id).val() == '') {
			$("#promosi_dealer_"+id).html("<option value=''>-- Pilih --</option>");
		} else {
			$("#promosi_dealer_"+id).empty();
			fillCombo(host+'master/combobox/tbl_master_dealer/', "promosi_dealer_"+id, "echo", $("#group_promosi_dealer_"+id).val());
		}
	}


	$('#tgl_mulai, #tgl_berakhir').datebox({
		onSelect: function(date){
			var mulai = $("#tgl_mulai").datebox('getValue');
			var akhir = $("#tgl_berakhir").datebox('getValue');

			var dmulai = new Date(mulai.split('-')[0],mulai.split('-')[1]-1,mulai.split('-')[2]);
			var dakhir = new Date(akhir.split('-')[0],akhir.split('-')[1]-1,akhir.split('-')[2]);
			var timeDiff = Math.abs(dakhir.getTime() - dmulai.getTime());
			var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
			
			var bulan = Math.floor(diffDays / 30);
			var hari  = Math.abs(diffDays % 30);
			
			$("#masa_berlaku_bln").val(bulan);
			$("#masa_berlaku_hari").val(hari);
		}
	});

	$("#all_dealer").change(function(resp) {
		if ($("#all_dealer").val() == "T") {
			$("#dealer").css({'background':'#ffffff'}).prop('disabled', false); 
		} else {
			$("#dealer").css({'background':'#cdcdcd'}).prop('disabled', true);
		}
	});

	$("#jenis_penjualan").change(function() {
		if ($("#jenis_penjualan").val()=="KL") {
			$("#pembiayaan").css({'background':'#ffffff'}).prop('disabled', false);
			$("#data_support").show();
			$("#kredit_leasing").show();
			$("#kredit_dealer").hide();
		} else if ($("#jenis_penjualan").val()=="KD" || $("#jenis_penjualan").val()=="T") {
			$("#pembiayaan").css({'background':'#cdcdcd'}).prop('disabled', true);
			$("#data_support").show();
			$("#kredit_dealer").show();
			$("#kredit_leasing").hide();
		} else {
			$("#pembiayaan").css({'background':'#cdcdcd'}).prop('disabled', true);
			$("#data_support").hide();
			$("#kredit_dealer").hide();
			$("#kredit_leasing").hide();
		}
	});

	$("#id_jenis_support").change(function() {
		if ($("#id_jenis_support").val()=="S") {
			$(".support_subsidi").show();
			$(".support_incentive").hide();
			$(".support_promo").hide();
		} else if ($("#id_jenis_support").val()=="I") {
			$(".support_subsidi").hide();
			$(".support_incentive").show();
			$(".support_promo").hide();
		} else if ($("#id_jenis_support").val()=="P") {
			$(".support_subsidi").hide();
			$(".support_incentive").hide();
			$(".support_promo").show();
		} else {
			$(".support_subsidi").hide();
			$(".support_incentive").hide();
			$(".support_promo").hide();
		}
	});
}

if (type == "aktiva"){
	$('#tanggal_pembelian').datebox({
		formatter:formatDate,
		value:tglpembelian
	});
	$('#tanggal_pemakaian').datebox({
		formatter:formatDate,
		value:tglpemakaian
	});
} 

if (type == "akun_kas") {
	$('#id_dealer').change(function() {
		if($('#id_dealer').val() == "") {
			$('#id_kas').html("<option value=''>-- Pilih --</option>");
		} else {
			$('#id_kas').empty();
			fillCombo(host+'master/combobox/tbl_master_kas/', 'id_kas', "echo", $('#id_dealer').val());
		}
	});

	$('#type_akun').change(function() {
		
		if($('#type_akun').val()=="") {
			$('#kode_akun_kas').val('');
		} else {
			$('#kode_akun_kas').val($('#type_akun').val());
		}
	});

	$('#bagian_dari_akun').change(function() {
		if($('#bagian_dari_akun').val()=="") {
			$('#kode_bagian_akun').val('');
		} else {
			$('#kode_bagian_akun').val($('#bagian_dari_akun').val());
		}
		
	});

	$('#bagian_dari_sub_akun').change(function() {
		if($('#bagian_dari_sub_akun').val()=="") {
			$('#kode_sub_akun').val('');
		} else {
			$('#kode_sub_akun').val($('#bagian_dari_sub_akun').val());
		}
	});


	if(editstatus == 'edit') {
		fillCombo(host+'master/combobox/tbl_master_dealer/'+id_dealer, 'id_dealer', "echo", $('#id_group_dealer').val(), function() {
			fillCombo(host+'master/combobox/tbl_master_kas/'+id_kas, 'id_kas', "echo", $('#id_dealer').val());
		});
		
		//fillCombo(host+'master/combobox/idx_kategori_barang/'+kategoribarang,'id_kategori_barang', "echo", $('#id_group_barang').val());
	}
} 

if (type == 'akun_barang_jasa') {
	if(editstatus == 'edit') {
		fillCombo(host+'master/combobox/idx_kategori_barang/'+kategoribarang,'id_kategori_barang', "echo", $('#id_group_barang').val());
	}
} 

if (type == 'akun_aktifa_tetap') {
	$('#kd_akun_aktiva').change(function() {
		$('#kode_akun_aktiva').val($('#kd_akun_aktiva').val());
	});
	$('#kd_akun_akum_susut').change(function() {
		$('#kode_akun_akum_susut').val($('#kd_akun_akum_susut').val());
	});
	$('#kd_akun_bebat_susut').change(function() {
		$('#kode_akun_bebat_susut').val($('#kd_akun_bebat_susut').val());
	});
	$('#open_balance_qty').change(function() {
		$('#kode_open_balance_qty').val($('#open_balance_qty').val());
	});

	if(editstatus == 'edit') {
		fillCombo(host+'master/combobox/tbl_master_dealer/'+id_dealer, 'id_dealer', "echo", $('#id_group_dealer').val());
	}
}